package com.cpi.cornpp.plantinitialcost.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.plantinitialcost.exception.PlantInitialCostException;
import com.cpi.cornpp.plantinitialcost.form.PlantInitialCostForm;
import com.cpi.cornpp.plantinitialcost.service.PlantInitialCostService;
import com.cpi.cornpp.plants.services.PlantService;
import com.cs.common.utils.NumberUtil;
/**
 * 
 * @author Edgar Joao
 * Plant Inicial Cost Actions
 *
 */
public class PlantInitialCostAction extends MappingDispatchAction {
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Id de version */
	private final static String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	
	private static final String PLANT_LIST = "plantList";//$NON-NLS-1$
	/**
	 * Show Plant Initial Cost.
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showPlantsInitialCost(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
		HttpSession httpSession = request.getSession(true);
		PlantInitialCostService plantInitialCostService = null;
		PlantService plantService = null;
		PlantInitialCostForm plantInitialCostForm = null;
		int versionId = 0;
		try{
			plantService = new PlantService();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			plantInitialCostService = new PlantInitialCostService();
			plantInitialCostForm = (PlantInitialCostForm) form;
			plantInitialCostForm.setPlantInicialCostList(plantInitialCostService.getPlantInicialCost(versionId));
			request.setAttribute(PLANT_LIST, plantService.list());
		}catch(PlantInitialCostException plantInitialCostException){
			throw plantInitialCostException;
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_SERVICE, PlantInitialCostException.ACTION_LISTS);
			throw plantInitialCostException;
		}		
		return mapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * Save Method.
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
		PlantInitialCostService plantInitialCostService = null;
		PlantInitialCostForm plantInitialCostForm = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{			
			httpSession = request.getSession();
			plantInitialCostService = new PlantInitialCostService();			
			plantInitialCostForm = (PlantInitialCostForm) form;
			messageResources = getResources(request);
			plantInitialCostService.updatePlantInitialCost(plantInitialCostForm.getPlantInicialCostList());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);						
		}catch(PlantInitialCostException plantInitialCostException){
			throw plantInitialCostException;
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_SERVICE, PlantInitialCostException.ACTION_LISTS);
			throw plantInitialCostException;
		}					
		return mapping.findForward(DEFAULT_FORWARD);
	}

}
